package com.cskaoyan.service;

import com.cskaoyan.bean.MarketAdmin;
import com.cskaoyan.bean.MarketAdminExample;
import com.cskaoyan.bean.common.BaseRespVo;
import com.cskaoyan.bean.common.CommonData;
import com.cskaoyan.mapper.MarketAdminMapper;
import com.cskaoyan.util.MyBatisUtil;
import com.github.pagehelper.PageHelper;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;

import java.util.List;

/**
 * @author stone
 * @date 2023/03/17 10:32
 */
public class AdminServiceImpl implements AdminService {
    SqlSessionFactory sqlSessionFactory = MyBatisUtil.getSqlSessionFactory();

    @Override
    public MarketAdmin query(String username, String password) {
        SqlSession sqlSession = sqlSessionFactory.openSession();
        MarketAdminMapper mapper = sqlSession.getMapper(MarketAdminMapper.class);
        MarketAdminExample example = new MarketAdminExample();
        example.createCriteria().andUsernameEqualTo(username).andPasswordEqualTo(password);
        List<MarketAdmin> marketAdmins = mapper.selectByExample(example);
        sqlSession.commit();
        if (marketAdmins != null && marketAdmins.size() == 1) {
            return marketAdmins.get(0);
        }
        return null;
    }
    @Override
    public BaseRespVo list(Integer page, Integer limit, String add_time, String oder,String username) {

        SqlSession sqlSession = sqlSessionFactory.openSession(true);
        PageHelper.startPage(page,limit);

        MarketAdminMapper mapper = sqlSession.getMapper(MarketAdminMapper.class);
        MarketAdminExample marketAdminExample = new MarketAdminExample();
        if (username!=null&&!"".equals(username))
            marketAdminExample.createCriteria().andUsernameEqualTo(username);
        List<MarketAdmin> marketAdmins = mapper.selectByExample(marketAdminExample);

        CommonData data = CommonData.data(marketAdmins);

        return BaseRespVo.ok(data);
    }

    @Override
    public BaseRespVo create(MarketAdmin admin) {
        SqlSession sqlSession = sqlSessionFactory.openSession(true);
        MarketAdminMapper mapper = sqlSession.getMapper(MarketAdminMapper.class);
        mapper.insert(admin);
        MarketAdminExample marketAdminExample = new MarketAdminExample();
        marketAdminExample.createCriteria().andUsernameEqualTo(admin.getUsername());
        List<MarketAdmin> marketAdmins = mapper.selectByExample(marketAdminExample);


        return BaseRespVo.ok(marketAdmins.get(0));
    }

    @Override
    public BaseRespVo updatet(MarketAdmin admin) {
        SqlSession sqlSession = sqlSessionFactory.openSession(true);
        MarketAdminMapper mapper = sqlSession.getMapper(MarketAdminMapper.class);

        mapper.updateByPrimaryKey(admin);
        MarketAdmin admin1 = mapper.selectByPrimaryKey(admin.getId());


        return BaseRespVo.ok(admin1);
    }

    @Override
    public BaseRespVo delete(MarketAdmin admin) {
        SqlSession sqlSession = sqlSessionFactory.openSession(true);
        MarketAdminMapper mapper = sqlSession.getMapper(MarketAdminMapper.class);
        mapper.deleteByPrimaryKey(admin.getId());
        return BaseRespVo.ok();
    }
}
